package com.manor.paradise.activity

import android.content.Intent
import android.net.Uri
import android.view.View
import com.gyf.immersionbar.ktx.immersionBar
import com.manor.common.activity.AbsActivity
import com.manor.common.http.JHttpCallback
import com.manor.common.utils.*
import com.manor.paradise.R
import com.manor.paradise.adapter.LybActiveIndexAdapter
import com.manor.paradise.bean.ReikInfoBean
import com.manor.paradise.bean.ReikindexBean
import com.manor.paradise.http.ElvesHttpUtil
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import com.yanzhenjie.permission.AndPermission
import com.yanzhenjie.permission.runtime.Permission
import kotlinx.android.synthetic.main.fragment_sub_reik.*
import kotlinx.android.synthetic.main.lyb_activity_active_index.*

class LybActiveIndexActivity : AbsActivity() {
    private var pageNum = 1
    private var mIsHire = true//是否已经养殖
    private var mIsAchieve = true//是否完成任务
    private var mIsAuth = false//是否实名认证
    private val mList = arrayListOf<ReikindexBean.RecordsBean>()
    private val mAdapter by lazy {
        LybActiveIndexAdapter(mList).apply {
            setOnItemClickListener { adapter, view, position ->
                LybHisInviteActivity.launch(mList[position])
            }
            setOnItemChildClickListener { adapter, view, position ->
                when(view.id){
                    R.id.sPhoneIv ->{
                        val phoneNum = getItem(position).mobile
                        callPhone(phoneNum)
                    }
                }
            }
        }
    }

    override fun getLayoutId() = R.layout.lyb_activity_active_index

    override fun main() {
        super.main()
        immersionBar {
            statusBarColor(R.color.white)
            titleBar(mTitleBarLayout)
            statusBarDarkFont(true)
        }
        setTopTitle("活跃度指数")
        selected(mYiyangzhiBtn, mWeiyangzhiBtn)
        mMyTeamRv.adapter = mAdapter
        selected(mAlreadyCompleteLayout, mNotCompleteLayout, mAlreadyAuthLayout, mNotNotAuthLayout)
        setListeners()
        initMode()
        mMyTeamSrl.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onLoadMore(refreshLayout: RefreshLayout) {
                pageNum++
                setData(false)
            }

            override fun onRefresh(refreshLayout: RefreshLayout) {
                pageNum = 1
                setData(true)
            }
        })
        setData(true)
    }
    private fun setListeners(){
        setClick(mYiyangzhiBtn, mWeiyangzhiBtn, mAlreadyCompleteLayout, mNotCompleteLayout, mAlreadyAuthLayout, mNotNotAuthLayout)
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when(v?.id){
            R.id.mYiyangzhiBtn ->{//已养殖
                selected(mYiyangzhiBtn, mWeiyangzhiBtn)
                mIsHire = true
                mIsAchieve = true
                mIsAuth = false
                mAlreadyCompleteLayout.v(true)
                mNotCompleteLayout.v(true)
                mAlreadyAuthLayout.v(false)
                mNotNotAuthLayout.v(false)
                selected(mAlreadyCompleteLayout, mNotCompleteLayout, mAlreadyAuthLayout, mNotNotAuthLayout)
            }
            R.id.mWeiyangzhiBtn ->{//未养殖
                selected(mWeiyangzhiBtn, mYiyangzhiBtn)
                mIsHire = false
                mIsAchieve = false
                mIsAuth = true
                mAlreadyCompleteLayout.v(false)
                mNotCompleteLayout.v(false)
                mAlreadyAuthLayout.v(true)
                mNotNotAuthLayout.v(true)
                selected(mAlreadyAuthLayout, mNotCompleteLayout, mAlreadyCompleteLayout, mNotNotAuthLayout)
            }
            R.id.mAlreadyCompleteLayout ->{//已完成任务
                selected(mAlreadyCompleteLayout, mNotCompleteLayout, mAlreadyAuthLayout, mNotNotAuthLayout)
                mIsAchieve = true
            }
            R.id.mNotCompleteLayout ->{//未完成任务
                selected(mNotCompleteLayout, mAlreadyCompleteLayout, mAlreadyAuthLayout, mNotNotAuthLayout)
                mIsAchieve = false
            }
            R.id.mAlreadyAuthLayout ->{//已实名
                selected(mAlreadyAuthLayout, mNotCompleteLayout, mAlreadyCompleteLayout, mNotNotAuthLayout)
                mIsAuth = true
            }
            R.id.mNotNotAuthLayout ->{//未实名
                selected(mNotNotAuthLayout, mNotCompleteLayout, mAlreadyCompleteLayout, mAlreadyAuthLayout)
                mIsAuth = false
            }
        }
        showWaitingDialog()
        setData(true)
    }

    private fun initMode() {
        ElvesHttpUtil.getGetInfo(object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                if (code != 200) {
                    ToastUtil.show(msg)
                    return
                }
                val reikindexBean: ReikInfoBean? = GsonUtil.gsonToBean<ReikInfoBean>(info, ReikInfoBean::class.java)
                mTeamActiveTv.text = reikindexBean?.teamAnima.toString()//我的灵气指数（全）
                mPersonalActiveTv.text = reikindexBean?.baseAnima.toString()
                mYouxiaoActiveTv.text = reikindexBean?.directHaveAnimaCount +"/"+reikindexBean?.teamCount
                mTuanduirenshuActiveTv.text = reikindexBean?.teamTotal.toString()
                mXiaoquActiveTv.text = reikindexBean?.unionAnima
            }
        })
    }

    fun setData(isRefresh :Boolean) {
        ElvesHttpUtil.getAnimaInfoNew(pageNum, mIsHire, mIsAchieve, mIsAuth, object : JHttpCallback() {
            override fun onSuccess(code: Int, msg: String, info: String) {
                dismissWaitingDialog()
                if (code != 200) {
                    ToastUtil.show(msg)
                    onError()
                    return
                }
                val reikindexBean: ReikindexBean = GsonUtil.gsonToBean<ReikindexBean>(info, ReikindexBean::class.java)
                KLog.e("json", reikindexBean)
                if (pageNum == 1) {
                    mList.clear()
                    mMyTeamSrl?.finishRefresh()
                }
                mList.addAll(reikindexBean.records)
                if (mList.isEmpty()) {
                    mAdapter.setEmptyView(R.layout.view_empty_default)
                }
                mAdapter.notifyDataSetChanged()
                mMyTeamSrl?.finishLoadMore()
            }
            override fun onError() {
                super.onError()
                dismissWaitingDialog()
                if (!isRefresh) {
                    pageNum--
                    mMyTeamSrl.finishLoadMore()
                }
            }
        })
    }
}